package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.ruoyi.base.domain.WorkingTechnologicalRel;
import com.ruoyi.base.mapper.WorkingTechnologicalRelMapper;
import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.base.vo.TechnologicalProcessVO;
import com.ruoyi.base.vo.TechnologicalProcessViewVo;
import com.ruoyi.base.vo.WorkingTechnologicalRelVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.TechnologicalProcessMapper;
import com.ruoyi.base.domain.TechnologicalProcess;
import com.ruoyi.base.service.ITechnologicalProcessService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工艺流程Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-16
 */
@Service
public class TechnologicalProcessServiceImpl implements ITechnologicalProcessService 
{
    @Autowired
    private TechnologicalProcessMapper technologicalProcessMapper;

    @Autowired
    private WorkingTechnologicalRelMapper workingTechnologicalRelMapper;

    /**
     * 查询工艺流程
     * 
     * @param id 工艺流程主键
     * @return 工艺流程
     */
    @Override
    public TechnologicalProcessViewVo selectTechnologicalProcessById(Long id) {
        TechnologicalProcess technologicalProcess = technologicalProcessMapper.selectTechnologicalProcessById(id);
        //获取工序信息
        List<WorkingTechnologicalRelVo> workingTechnologicalRelVo = technologicalProcessMapper.getWorkingTechnologicalRelVo(technologicalProcess.getProcessNo());
        TechnologicalProcessViewVo  viewVo= new TechnologicalProcessViewVo();
        BeanUtils.copyProperties(technologicalProcess,viewVo);
        viewVo.setRel(workingTechnologicalRelVo);
        return viewVo;
    }

    /**
     * 查询工艺流程列表
     * 
     * @param technologicalProcess 工艺流程
     * @return 工艺流程
     */
    @Override
    public List<TechnologicalProcess> selectTechnologicalProcessList(TechnologicalProcess technologicalProcess) {
        List<TechnologicalProcess> technologicalProcesses = technologicalProcessMapper.selectTechnologicalProcessList(technologicalProcess);
        return technologicalProcesses;
    }

    /**
     * 新增工艺流程
     * 
     * @param technologicalProcessVO 工艺流程
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTechnologicalProcess(TechnologicalProcessVO technologicalProcessVO) {
        TechnologicalProcess technologicalProcess = new TechnologicalProcess();
        BeanUtils.copyProperties(technologicalProcessVO,technologicalProcess);

        technologicalProcess.setProcessNo(GenCodeUtils.getProcessNo());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        technologicalProcess.setCreateBy(user.getUserName());
        technologicalProcess.setCreateRole(user.getRoles().get(0).getRoleName());
        technologicalProcess.setCreateTime(DateUtils.getNowDate());

        //设置工序数量
        technologicalProcess.setProcessQuantity(Long.valueOf((technologicalProcessVO.getRel().size())));


        for (WorkingTechnologicalRel rel : technologicalProcessVO.getRel()) {
            rel.setTechnologicalNo(technologicalProcess.getProcessNo());
            workingTechnologicalRelMapper.insertWorkingTechnologicalRel(rel);
        }
        //设置工艺流程
        String tech = workingTechnologicalRelMapper.getTechnologicalProcess(technologicalProcess.getProcessNo());
        technologicalProcess.setTechnologicalProcess(tech);

        return technologicalProcessMapper.insertTechnologicalProcess(technologicalProcess);
    }

    /**
     * 修改工艺流程
     * 
     * @param technologicalProcessVO 工艺流程
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTechnologicalProcess(TechnologicalProcessVO technologicalProcessVO) {
        TechnologicalProcess technologicalProcess = new TechnologicalProcess();
        BeanUtils.copyProperties(technologicalProcessVO,technologicalProcess);
        //工艺流程表-工序表-解除关系
        //working_technological_rel
        workingTechnologicalRelMapper.deleteWorkingTechnologicalRelByProcessNo(technologicalProcess.getProcessNo());
        //维护关系
        for (WorkingTechnologicalRel rel : technologicalProcessVO.getRel()) {
            rel.setTechnologicalNo(technologicalProcess.getProcessNo());
            workingTechnologicalRelMapper.insertWorkingTechnologicalRel(rel);
        }
        //设置工艺流程
        String tech = workingTechnologicalRelMapper.getTechnologicalProcess(technologicalProcess.getProcessNo());
        technologicalProcess.setTechnologicalProcess(tech);
        //设置工序数量
        technologicalProcess.setProcessQuantity(Long.valueOf((technologicalProcessVO.getRel().size())));

        //修改人，修改时间
        SysUser user = SecurityUtils.getLoginUser().getUser();
        technologicalProcess.setUpdateBy(user.getUserName());
        technologicalProcess.setUpdateTime(DateUtils.getNowDate());

        return technologicalProcessMapper.updateTechnologicalProcess(technologicalProcess);
    }

    /**
     * 批量删除工艺流程
     * 
     * @param ids 需要删除的工艺流程主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTechnologicalProcessByIds(Long[] ids) {
        for (Long id : ids) {
            //工艺流程表-工序表-解除关系
            TechnologicalProcess technologicalProcess = technologicalProcessMapper.selectTechnologicalProcessById(id);
            //working_technological_rel
            workingTechnologicalRelMapper.deleteWorkingTechnologicalRelByProcessNo(technologicalProcess.getProcessNo());
        }
        return technologicalProcessMapper.deleteTechnologicalProcessByIds(ids);
    }

    /**
     * 删除工艺流程信息
     * 
     * @param id 工艺流程主键
     * @return 结果
     */
    @Override
    public int deleteTechnologicalProcessById(Long id) {

        return technologicalProcessMapper.deleteTechnologicalProcessById(id);
    }



}
